package action;

import java.util.List;

import bean.Publisher;
import dao.PublisherDAO;

public class PublisherAction extends SuperAction {
	private static final long serialVersionUID = 1L;

	// for admin only
	public String deletePublisher() {

		try {
			if (!session.getAttribute("employee").equals("true"))
				throw new Exception();
		} catch (Exception e) {
			return "failed_page";
		}

		int publihsherid = Integer
				.parseInt(request.getParameter("publisherid"));
		PublisherDAO dao = new PublisherDAO();
		try {
			dao.deletePublisher(publihsherid);
			return "deletePublisher_succ";
		} catch (Exception e) {
		}
		return "failed_page";
	}

	// for admin only
	public String addPublisher() {
		try {
			if (!session.getAttribute("employee").equals("true"))
				throw new Exception();
		} catch (Exception e) {
			return "failed_page";
		}

		try {
			String name = request.getParameter("name");
			String linkman = request.getParameter("linkman");
			String address = request.getParameter("address");
			String email = request.getParameter("email");
			String phone = request.getParameter("phone");
			String website = request.getParameter("website");

			Publisher publisher = new Publisher();
			publisher.setAddress(address);
			publisher.setEmail(email);
			publisher.setLinkman(linkman);
			publisher.setName(name);
			publisher.setPhone(phone);
			publisher.setWebsite(website);

			PublisherDAO dao = new PublisherDAO();
			dao.addPublisher(publisher);

			return "addPublisher_succ";

		} catch (Exception e) {
		}
		return "failed_page";
	}

	// for admin only
	public String updatePublisher() {
		try {
			if (!session.getAttribute("employee").equals("true"))
				throw new Exception();
		} catch (Exception e) {
			return "failed_page";
		}

		try {
			int publisherid = Integer.parseInt(request
					.getParameter("publisherid"));
			String name = request.getParameter("name");
			String linkman = request.getParameter("linkman");
			String address = request.getParameter("address");
			String email = request.getParameter("email");
			String phone = request.getParameter("phone");
			String website = request.getParameter("website");
			PublisherDAO dao = new PublisherDAO();
			List<Publisher> list = dao.queryPublisherById(publisherid);
			if (list.size() > 0) {
				Publisher publisher = list.get(0);
				publisher.setAddress(address);
				publisher.setEmail(email);
				publisher.setLinkman(linkman);
				publisher.setName(name);
				publisher.setPhone(phone);
				publisher.setPublisherid(publisherid);
				publisher.setWebsite(website);
				dao.updatePublisher(publisher);
				return "updatePublisher_suc";
			}
		} catch (Exception e) {
		}
		return "failed_page";
	}

	// for admin only
	public String managePublishers() {
		try {
			if (!session.getAttribute("employee").equals("true"))
				throw new Exception();
		} catch (Exception e) {
			return "failed_page";
		}

		PublisherDAO pDao = new PublisherDAO();
		List<Publisher> list = pDao.queryAllPublisher();
		session.setAttribute("searchResult", list);
		return "manage_publishes";
	}

	// for admin only
	public String publisherSearch() {
		try {
			if (!session.getAttribute("employee").equals("true"))
				throw new Exception();
		} catch (Exception e) {
			return "failed_page";
		}

		try {
			request.setCharacterEncoding("utf-8");
			String publisherName = request.getParameter("name");
			String linkman = request.getParameter("linkman");
			String address = request.getParameter("address");
			String email = request.getParameter("email");
			String phone = request.getParameter("phone");
			String website = request.getParameter("website");

			String sql = "PUBLISHERID <> 0";

			if (!publisherName.trim().equals("")) {
				sql += " and name like'%" + publisherName + "%'";
			}

			if (!linkman.trim().equals("")) {
				sql += " and linkman like'%" + linkman + "%'";
			}
			if (!address.trim().equals("")) {
				sql += " and address like'%" + address + "%'";
			}
			if (!email.trim().equals("")) {
				sql += " and email like'%" + email + "%'";
			}
			if (!phone.trim().equals("")) {
				sql += " and phone like'%" + phone + "%'";
			}
			if (!website.trim().equals("")) {
				sql += " and website like'%" + website + "%'";
			}

			PublisherDAO dao = new PublisherDAO();
			List<Publisher> list = dao.queryPublisherBySql(sql);
			session.setAttribute("searchResult", list);
			return "searchPublisher_result";

		} catch (Exception e) {
		}
		return "searchPublisher_fail";
	}

}
